package tech.aistar.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.aistar.constants.MessageConstant;
import tech.aistar.mapper.CartMapper;
import tech.aistar.mapper.OrderMapper;
import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.pojo.Flower;
import tech.aistar.pojo.Order;
import tech.aistar.pojo.ShoppingCart;
import tech.aistar.pojo.dto.CartPayDTO;
import tech.aistar.pojo.result.ResultReturn;
import tech.aistar.service.CartService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Map findByUserId(int user_id) {
        ShoppingCart byUserId = cartMapper.findByUserId(user_id);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(byUserId);
        stringObjectMap.put("code", 200);
        return stringObjectMap;
    }

    @Override
    public Integer fingByUserIdAndFlowerId(int user_id, int flower_id) {
        return cartMapper.findByUserIdAndFlowerId(user_id, flower_id);
    }

    @Override
    public int deleteByUserIdAndFlowerId(int user_id, int flower_id) {
        return cartMapper.deleteByUserIdAndFlowerId(user_id,flower_id);
    }

    @Override
    public int insert(int userId, int flowerId, int count) {
        return cartMapper.insert(userId,flowerId,count);
    }

    @Override
    public int updateCount(int userId, int flowerId, int count) {
        return cartMapper.updateCount(userId,flowerId,count);
    }

    @Override
    public int deleteByUserIdAndFlowerIds(int user_id, String ids) {
        return cartMapper.deleteByUserIdAndFlowerIds(user_id,ids);
    }

    @Override
    public List<Integer> getFlowerIdsByUserId(int user_id) {
        return cartMapper.getFlowerIdsByUserId(user_id);
    }

    @Override
    public List<Integer> getFlowerCountsByUserId(int user_id) {
        return cartMapper.getFlowerCountsByUserId(user_id);
    }

    @Override
    public ResultReturn pay(CartPayDTO cartPayDTO) {

        //flowers
        List<Flower> flowers = new ArrayList<>();
        cartPayDTO.getIds().forEach(id->{
            Flower flower = new Flower();
            flower.setFlowerId(id);
            flowers.add(flower);
        });

        //counts
        List<Integer> counts = new ArrayList<>();
        cartPayDTO.getIds().forEach(id->{
            Integer count = cartMapper.findByUserIdAndFlowerId(cartPayDTO.getUserId(), id);
            System.out.println(count);
            counts.add(cartMapper.findByUserIdAndFlowerId(cartPayDTO.getUserId(), id));
            cartMapper.deleteByUserIdAndFlowerId(cartPayDTO.getUserId(),id);
        });

        //address
        String address = userInfoMapper.selectInfoByUser_id(cartPayDTO.getUserId()).getAddress();

        Order order = new Order();
        order.setStatus(0);
        order.setUser_id(cartPayDTO.getUserId());
        order.setNote(cartPayDTO.getNote());
        order.setSend_time(cartPayDTO.getSendTime());
        order.setOrder_id(StrUtil.uuid());
        order.setFlowers(flowers);
        order.setCounts(counts);
        order.setAddress(address);
        Integer result = orderMapper.initOrder(order);
        if (result != null) {
            return ResultReturn.success();
        }
        return ResultReturn.fail(MessageConstant.FAIL);
    }
}
